CC            = gcc
AR            = ar
BUILT_DIR     = built
TARGET        = app
LIB           = vlib

INCLUDE       = -I ./source
LIBSRC        = $(wildcard source/*.c source/config/*.c)
LIBHEAD       = $(wildcard source/*.h source/config/*.h)
TESTSRC       = $(wildcard main.c test/*.c)
SOURCES       += $(LIBSRC)
SOURCES       += $(TESTSRC)

OBJS          = $(patsubst %.c, $(BUILT_DIR)/%.o, $(SOURCES))
LIBO          = $(patsubst %.c, $(BUILT_DIR)/%.o, $(LIBSRC))
TAR_PATH      = $(BUILT_DIR)/app
LIBN          = lib$(LIB)
LIBA          = $(BUILT_DIR)/$(LIBN).a


# link
${TAR_PATH}:$(OBJS)
	@ $(CC) $(OBJS) -o $(TAR_PATH)

# compile
$(BUILT_DIR)/%.o:%.c
	$(shell mkdir -p $(dir $@))
	@ echo "compiling $(notdir $<)"
	@ $(CC) $(INCLUDE) -c $< -o $@

install: 
	@ $(AR) -crv $(LIBA) $(LIBO)
	@ mv $(LIBA) /usr/lib
	@ mkdir $(LIB)
	@ mkdir $(LIB)/config
	@ cp $(wildcard source/*.h) $(LIB)
	@ cp $(wildcard source/config/*.h) $(LIB)/config
	@ sudo mv $(LIB) /usr/include
	@ sudo ldconfig
	
# gcc main.c -L. -lvlib -o main

uninstall: 
	@ sudo rm /usr/lib/$(LIBN).a
	@ sudo rm /usr/include/$(LIB) -rf
	@ sudo ldconfig
	
.PHONY:clean
clean:
	@echo "remove app and objs files ..."
	$(shell rm $(BUILT_DIR)/* -rf)
